package com.miyake.loveviewweb.client.plugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.client.service.DataServiceAsync;
import com.miyake.loveviewweb.shared.model.DataColumnModel;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.PluginModel;
import com.miyake.loveviewweb.shared.model.ValueModel;

public class PluginSettingPanel extends Composite {

    private VerticalPanel panel = new VerticalPanel();
    private PluginModel pluginModel;
    private List<ListBox> listBoxes = new ArrayList<ListBox>();
//    private List<ValueModel> values;
    
    public PluginSettingPanel(PluginModel plugin, final DataModel dataModel) {
        panel.setWidth("100%");
        initWidget(panel);
        DataServiceAsync service = DataService.Util.getInstance();
        service.getPlugin(plugin.getKey(), new AsyncCallback<PluginModel>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(PluginModel result) {
                pluginModel = result;
                getPanel(pluginModel, dataModel);
            }
            
        });

    }
    private void getPanel(PluginModel pluginModel, DataModel dataModel) { 
        for (String header : pluginModel.getHeaders()) {
            HorizontalPanel p = new HorizontalPanel();
            p.add(new Label(header));
            p.add(getDataSelection(header, dataModel));
            panel.add(p);
        }
        
        for (final ValueModel value : pluginModel.getValues()) {
            HorizontalPanel p = new HorizontalPanel();
            p.add(new HTML(value.getCaption()));
            final TextBox textArea = new TextBox();
            textArea.setWidth("100px");
            textArea.setText(value.getValue());
            p.add(textArea);
            if (!value.getSubUnit().equals("None")) {
                p.add(new HTML(value.getSubUnit().toString()));
            }
            p.add(new HTML(value.getUnit()));
            panel.add(p);
            
            textArea.addChangeHandler(new ChangeHandler() {

                public void onChange(ChangeEvent event) {
                    value.setValue(textArea.getText());
                }
                
            });
        }            
    }
    private Widget getDataSelection(String header, DataModel dataModel) {
        final ListBox listBox = new ListBox();
        listBox.setName(header);
        if (dataModel.isContent()) {
            buildComboBox(dataModel, listBox);
        }
        else {
            DataService.Util.getInstance().getData(dataModel.getKey(), true, new AsyncCallback<DataModel>() {
                public void onFailure(Throwable caught) {
 
                }

                public void onSuccess(DataModel result) {
                    buildComboBox(result, listBox);
                }
                
            });
        }
        return listBox;
    }
    
    private void buildComboBox(DataModel dataModel, final ListBox listBox) {
        listBox.addItem("");
        for (DataColumnModel dataColumnModel : dataModel.getXyDataBody().getDataColumn()) {
            listBox.addItem(dataColumnModel.getHeader());
            
            listBoxes.add(listBox);
        }
    }
    /**
     * @return the values
     */
    public List<ValueModel> getValues() {
        return pluginModel.getValues();
    }

    public Map<String, String> getData() {
        Map<String, String> ret = new HashMap<String, String>();
        for (ListBox listBox : listBoxes) {
            ret.put(listBox.getName(), listBox.getItemText(listBox.getSelectedIndex()));
        }
        return ret;
    }

}
